package com.gmail.blackdog1987.carcare.widget;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.gmail.blackdog1987.carcare.R;

/**
 * 照片选择
 * 
 * @author 董国超
 * @date 2014年1月17日
 */
public class SelectPicPopupWindow extends Activity implements OnClickListener {

	private Button mTakePhoto, mPickPhoto, mCancle;
	private LinearLayout mWrapper;
	private Intent mRecieveIntent;
	private static final int PHOTO_REQUEST_TAKEPHOTO = 1;// 拍照
	private static final int PHOTO_REQUEST_GALLERY = 2;// 从相册中选择
	private static final int PHOTO_REQUEST_CUT = 3;// 结果
	private File mTempFiel = new File(Environment.getExternalStorageDirectory(), getPhotoFileName());

	public static final int TYPE_CUT = 1;
	public static final int TYPE_NORMAL = 0;

	public static final String INTENT_EXTRA_KEY_TYPEMODE = "extra_typemodel";

	private int mTypeMode;
	
	private int aspectX = 1;
	private int aspectY = 1;
	private int outputX = 640;
	private int outputY = 640;
	
	private static final String IMAGE_FILE_LOCATION = "file:///sdcard/temp.jpg";//temp file
	Uri imageUri = Uri.parse(IMAGE_FILE_LOCATION);//The Uri to store the big bitmap

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.widget_pic_select);
		mRecieveIntent = getIntent();
		mTypeMode = mRecieveIntent.getIntExtra(INTENT_EXTRA_KEY_TYPEMODE, TYPE_NORMAL);
		aspectX = mRecieveIntent.getIntExtra("aspectX", 1);
		aspectY = mRecieveIntent.getIntExtra("aspectY", 1);
		outputX = mRecieveIntent.getIntExtra("outputX", 640);
		outputY = mRecieveIntent.getIntExtra("outputY", 640);
		String uri = mRecieveIntent.getStringExtra("uri");
		if(uri!=null&&uri.length()!=0){
			imageUri = Uri.parse(uri);
		}
		
		mTakePhoto = (Button) this.findViewById(R.id.btn_take_photo);
		mPickPhoto = (Button) this.findViewById(R.id.btn_pick_photo);
		mCancle = (Button) this.findViewById(R.id.btn_cancel);

		mWrapper = (LinearLayout) findViewById(R.id.pop_layout);

		mWrapper.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				Toast.makeText(getApplicationContext(), "提示：点击窗口外部关闭窗口！", Toast.LENGTH_SHORT).show();
			}
		});
		mCancle.setOnClickListener(this);
		mPickPhoto.setOnClickListener(this);
		mTakePhoto.setOnClickListener(this);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		finish();
		return true;
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {

		switch (requestCode) {
		case PHOTO_REQUEST_TAKEPHOTO:
			startPhotoZoom(Uri.fromFile(mTempFiel), 150);
			break;

		case PHOTO_REQUEST_GALLERY:
			if (data != null)
				startPhotoZoom(data.getData(), 150);
			break;

		case PHOTO_REQUEST_CUT:
			if (data != null) {
//				data.setData(imageUri);
//				mRecieveIntent.putExtras(data);
				mRecieveIntent.setData(imageUri);
//				mRecieveIntent.putExtra("uri", imageUri);
				setResult(Activity.RESULT_OK, mRecieveIntent);
				finish();
			}
			break;
		}
		
		super.onActivityResult(requestCode, resultCode, data);

	}

	private void startPhotoZoom(Uri uri, int size) {
		Intent intent = new Intent("com.android.camera.action.CROP");
		intent.setDataAndType(uri, "image/*");
		intent.putExtra("crop", "true");
		intent.putExtra("scale", true);
		intent.putExtra("return-data", false);
		intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
		intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
		intent.putExtra("noFaceDetection", true); // no face detection
//		if (mTypeMode == TYPE_CUT) {
//			intent.putExtra("aspectX", 1);
//			intent.putExtra("aspectY", 1);
//
//			intent.putExtra("outputX", size);
//			intent.putExtra("outputY", size);
//		} else {
//			// intent.putExtra("outputX", getWindowManager().getDefaultDisplay().getWidth());
//			// intent.putExtra("outputY", getWindowManager().getDefaultDisplay().getHeight());
//		}
//		intent.putExtra("return-data", true);
		
		// 设置裁剪    
        // aspectX aspectY 是宽高的比例    
//        intent.putExtra("aspectX", aspectX);    
//        intent.putExtra("aspectY", aspectY);    
        // outputX outputY 是裁剪图片宽高    
//        intent.putExtra("outputX", outputX);    
//        intent.putExtra("outputY", outputY);    
//        intent.putExtra("return-data", true);    

		startActivityForResult(intent, PHOTO_REQUEST_CUT);
//		
//		
//		Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
//		intent.setType("image/*");
//		intent.putExtra("crop", "true");
//		intent.putExtra("aspectX", 2);
//		intent.putExtra("aspectY", 1);
//		intent.putExtra("outputX", 600);
//		intent.putExtra("outputY", 300);
//		intent.putExtra("scale", true);
//		intent.putExtra("return-data", false);
//		intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
//		intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
//		intent.putExtra("noFaceDetection", true); // no face detection
//		startActivityForResult(intent, CHOOSE_BIG_PICTURE);
	}

	private String getPhotoFileName() {
		Date date = new Date(System.currentTimeMillis());
		SimpleDateFormat dateFormat = new SimpleDateFormat("'IMG'_yyyyMMdd_HHmmss", Locale.getDefault());
		return dateFormat.format(date) + ".jpg";
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_take_photo:
			Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
			intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mTempFiel));
			startActivityForResult(intent, PHOTO_REQUEST_TAKEPHOTO);
			break;
		case R.id.btn_pick_photo:
			Intent intent2 = new Intent(Intent.ACTION_PICK, null);
			intent2.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
			startActivityForResult(intent2, PHOTO_REQUEST_GALLERY);
			break;
		case R.id.btn_cancel:
			finish();
			break;
		default:
			break;
		}

	}

}
